package com.czr.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.czr.constant.isLogin;
import com.czr.dao.entity.CzrColumn;
import com.czr.dao.service.CzrColumnService;
import com.czr.dao.service.CzrNewsService;
import com.czr.dto.ColumnParam;
import com.czr.dto.CzrNewsParam;
import com.czr.dto.NewsParam;
import com.czr.dto.Search;
import com.czr.vo.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@Slf4j
@Api(value = "新闻资讯", tags = "新闻资讯")
@RestController
@RequestMapping("/news")
public class CzrNewsController {

    @Autowired
    private CzrNewsService czrNewsService;

    @Autowired
    private CzrColumnService czrColumnService;

    @Resource
    private RedisTemplate<Object, Object> redisTemplate;

    @isLogin
    @ApiOperation(value = "新闻列表", notes = "新闻列表")
    @PostMapping("/list")
    public Result<Object> list(@RequestBody NewsParam search) {
        try {
            return czrNewsService.list(search);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail();
        }

    }
    @isLogin
    @ApiOperation(value = "根据栏目id查询子栏目", notes = "根据栏目id查询子栏目")
    @GetMapping("/getChildColumn")
    public Result<Object> getChildColumn(@RequestParam Integer columnId) {
        try {
            return czrNewsService.getNewsInfoByNewsId(columnId);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail();
        }

    }
    @ApiOperation(value = "新增或子栏目", notes = "新增或子栏目")
    @PostMapping("/addChildColumn")
    public Result<Object> addChildColumn(@RequestBody CzrColumn column) {
        try {
            return czrNewsService.addChildColumn(column);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail();
        }

    }
    @isLogin
    @ApiOperation(value = "栏目列表", notes = "栏目列表")
    @PostMapping("/getColumnList")
    public Result<Object> getColumnList(@RequestBody ColumnParam search) {
        try {
            return czrColumnService.list(search);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail();
        }

    }
    @ApiOperation(value = "发布新闻/修改新闻", notes = "发布新闻/修改新闻")
    @PostMapping("/addOrUpdate")
    public Result<Map<String, Object>> addOrUpdate(@RequestBody CzrNewsParam czrNewsParam, HttpServletRequest request) {
        try {
            ValueOperations<Object, Object> valueOperations = redisTemplate.opsForValue();
            Object object=valueOperations.get(request.getHeader("token"));
            if (!StringUtils.isEmpty(object)) {
                JSONObject cacheToken = JSON.parseObject(object.toString());
                return  czrNewsService.addOrUpdate(czrNewsParam,Integer.parseInt(cacheToken.get("id").toString()));
            }else{
                return Result.success(202,"请登陆后发布新闻!");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return Result.success(201,"提交失败!");
        }

    }
    @isLogin
    @ApiOperation(value = "查看新闻详情", notes = "查看新闻详情")
    @ApiImplicitParams({
            //参数效验
            @ApiImplicitParam(name="newsId",value="新闻id",required=true,dataType="Long")
    })
    @PostMapping("/getNewsInfo")
    public Result<Object> getNewsInfo(@RequestParam Long newsId) {
        try {
            Result<Object> result = czrNewsService.getNewsInfo(newsId);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail();
        }

    }
    @ApiOperation(value = "管理端-新闻列表", notes = "管理端-新闻列表")
    @PostMapping("/glList")
    public Result<Object> glList(@RequestBody NewsParam search) {
        try {
            return czrNewsService.glList(search);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail();
        }

    }
    @ApiOperation(value = "管理端-删除新闻", notes = "管理端-删除新闻")
    @GetMapping("/delNews")
    public Result<Map<String, Object>> delNews(@RequestParam Integer newsId, HttpServletRequest request) {
        try {
            ValueOperations<Object, Object> valueOperations = redisTemplate.opsForValue();
            Object object=valueOperations.get(request.getHeader("token"));
            if (!StringUtils.isEmpty(object)) {
                JSONObject cacheToken = JSON.parseObject(object.toString());
                return  czrNewsService.delNews(newsId,cacheToken);
            }else{
                return Result.fail("token异常");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail();
        }

    }

}
